<template>
  <div class="home">
    <div class="container">
      <el-form :inline="true" :model="form" :rules="rules" ref="form" class="form" label-width="120px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"></el-input>
        </el-form-item>
      </el-form>

      <el-button type="primary" @click="update">修改</el-button>

    </div>
  </div>
</template>

<script>

export default {
  name: 'EditCategory',
  data() {
    return {
      form: {},
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' },
        ],
        remark: [
          { required: true, message: '请输入备注', trigger: 'blur' },
        ],
      }
    }
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      const id = this.$route.query.id;
      this.request.get("/api/category/" + id).then(res => {
        this.form = res.data;
      })
    },
    update() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.request.put("/api/category/update", this.form).then(res => {
            if (res.code === '200') {
              this.$notify.success("更新成功");
            } else {
              this.$notify.error("更新失败");
            }

            this.form = {};
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>


<style lang="scss" scoped>
.home {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;

  .container {
    width: 800px;
    height: 500px;

    margin-top: 80px;
    background-color: #ffffff;
    border-radius: 25px;
    box-shadow: 0 3px 5px #ccc;
    display: flex;
    flex-direction: column;
    justify-content: space-evenly;
    align-items: center;

    .form {
      display: flex;
      flex-wrap: wrap;
      justify-content: center;
    }
  }
}
</style>